package com.linxiao.call.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Resource;

import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.linxiao.call.domain.CrmCallBackCdr;
import com.linxiao.call.domain.CrmOutCallCdr;
import com.linxiao.call.mapper.CrmOutCallCdrMapper;
import com.linxiao.call.service.ICrmCallBackCdrService;
import com.linxiao.call.service.ICrmOutCallCdrService;
import com.linxiao.call.vo.CrmCallRecordCallBackVo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.*;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.CrmConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.system.domain.SysDeptToken;
import com.ruoyi.system.manager.AdminManager;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDeptTokenService;
import com.ruoyi.system.service.ISysUserService;


/**
 * 【外呼通话记录】Service业务层处理
 *
 * @author qiqi
 * @date 2022-04-08
 */
@Service
public class CrmOutCallCdrServiceImpl implements ICrmOutCallCdrService {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private CrmOutCallCdrMapper crmOutCallCdrMapper;
    @Autowired
    private ICrmCallBackCdrService crmCallBackCdrService;
    @Autowired
    private ISysUserService sysUserService;
    @Resource
    private AdminManager adminManager;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysDeptTokenService sysDeptTokenService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private RedisCache redisCache;

    @Value("${tr.call.records.region:null}")
    private String region;
    @Value("${tr.call.records.version:null}")
    private String version;
    @Value("${api.url.idhub:null}")
    private String idhubUrl;
    @Value("${api.file.upload.url:null}")
    private String fileUploadUrl;
    @Value("${api.data.encrypt.key:null}")
    private String dataEncryptKey;
    @Value("${tr.token:null}")
    private String trToken;
    @Value("${url.upload}")
    private String uploadUrl;

    /**
     * 查询【获取外呼通话记录详情】
     *
     * @param id 【详情】主键
     * @return 【结果】
     */
    @Override
    @DataScope(deptAlias = "c")
    public CrmOutCallCdr selectCrmOutCallCdrById(Long id, String cdrMainUniqueId) {
        CrmOutCallCdr crmOutCallCdr = crmOutCallCdrMapper.selectCrmOutCallCdrById(id, cdrMainUniqueId);
        if (Objects.nonNull(crmOutCallCdr)) {
            crmOutCallCdr.setCdrId(crmOutCallCdr.getId() + "");
        }
        else {
            throw new UtilException("获取通话详情失败");
        }
        return crmOutCallCdr;
    }

    /**
     * 查询【外呼通话记录】列表
     *
     * @param crmOutCallCdr 【外呼通话记录实体】
     * @return 【请填写功能名称】集合
     */
    @Override
    @DataScope(deptAlias = "c")
    public List<CrmOutCallCdr> selectCrmOutCallCdrList(CrmOutCallCdr crmOutCallCdr) {
        List<CrmOutCallCdr> crmOutCallCdrs;
        LoginUser user = SecurityUtils.getLoginUser();
        // 如果是管理岗位，取所管理的部门，如果不是管理岗位，取个人外呼记录
        boolean result = adminManager.loginUserIsLeader(user);
        if (!result) {
            crmOutCallCdr.setUserId(user.getUserId());
        }
        // 如果是分公司管理员或者质检，取分公司所有数据
        if (adminManager.loginUserIsBranchBoss(user) && !"super".equals(user.getUsername())) {
            crmOutCallCdr.setMchId(user.getUser().getMchId());
            if(crmOutCallCdr.getUserId() != null){
                crmOutCallCdr.setUserId(crmOutCallCdr.getUserId());
            }
        }

        //客服质检 电销 看全公司
        if (adminManager.loginUserIsServiceDept(user) || user.getUser().getMchId().intValue() == 1063) {
            crmOutCallCdr.setUserId(null);
            crmOutCallCdr.setMchId(user.getUser().getMchId());
            if (crmOutCallCdr.getParams() !=null && crmOutCallCdr.getParams().get("dataScope")!=null) {
                crmOutCallCdr.getParams().put("dataScope", "");
            }            
        }
        
        // 质检  开全集团的
        if ( user.getUser().getMchId().intValue() == 1090) {
            crmOutCallCdr.setUserId(null);
            crmOutCallCdr.setMchId(null);
            if (crmOutCallCdr.getParams() !=null && crmOutCallCdr.getParams().get("dataScope")!=null) {
                crmOutCallCdr.getParams().put("dataScope", "");
            }
        }
        // 查询数据库
        PageUtils.startPage();
        crmOutCallCdrs = crmOutCallCdrMapper.selectCrmOutCallCdrList(crmOutCallCdr);
        crmOutCallCdrs.stream().forEach(item -> item.setCdrId(String.valueOf(item.getId())));
        return crmOutCallCdrs;
    }

    /**
     * 新增【外呼通话记录】
     *
     * @param crmOutCallCdr 【外呼实体】
     * @return 结果
     */
    @Override
    public AjaxResult insertCrmOutCallCdr(CrmOutCallCdr crmOutCallCdr) {
        try {
            // 根据工号 获取二级部门id 和 当前员工所在部门ID
            if (StringUtils.isNotEmpty(crmOutCallCdr.getCdrCno())) {
                SysUser sysUser = sysUserService.selectUserByAgentId(crmOutCallCdr.getCdrCno());
                if (Objects.nonNull(sysUser)) {
                    crmOutCallCdr.setMchId(sysUser.getMchId());
                    crmOutCallCdr.setDeptId(sysUser.getDeptId());
                    crmOutCallCdr.setUserId(sysUser.getUserId());
                }
            }
            crmOutCallCdr.setId(IdSeqGenerator.getInstance().getId());
            crmOutCallCdr.setStartTime(DateUtils.getTime());
            String cdrCustomerNumber = crmOutCallCdr.getCdrCustomerNumber();
            // 加密存储 手机号
            // 脱敏手机号
            crmOutCallCdr.setMobileHidden(DesensitizedUtil.mobilePhone(cdrCustomerNumber));
            int i = crmOutCallCdrMapper.insertCrmOutCallCdr(crmOutCallCdr);
            if (i > 0) {
                return AjaxResult.success();
            }
        } catch (Exception e) {
            log.error("新增外呼通话记录失败 e={}", e);
        }
        return AjaxResult.error();
    }

    @Override
    public int batchInsertCrmOutCallCdr(List<CrmOutCallCdr> crmOutCallCdrList) {
        return crmOutCallCdrMapper.batchInsertCrmOutCallCdr(crmOutCallCdrList);
    }

    /**
     * 批量删除重复数据
     *
     * @param crmOutCallCdr
     * @return
     */
    @Override
    public AjaxResult cdrBatchUpdateRecord(CrmOutCallCdr crmOutCallCdr) {
        try {
            // 获取所有的重复的  【cdrMainUniqueId】
            List<CrmOutCallCdr> crmOutCallCdrListAll = crmOutCallCdrMapper.getAllRepeatDataUniqueId();
            List<List<CrmOutCallCdr>> lists = splitList(crmOutCallCdrListAll, 1000);
            List<CrmOutCallCdr> crmOutCallCdrList = new ArrayList<>();
            for (List<CrmOutCallCdr> cdrs : lists) {
                // 获取 全局不重复数据
                List<CrmOutCallCdr> crmOutCallCdrListTotal = crmOutCallCdrMapper.getRepeatData(cdrs);
                crmOutCallCdrList.addAll(crmOutCallCdrListTotal);
            }


            if (crmOutCallCdrList.isEmpty()) {
                log.info("cdrBatchUpdateRecord.isEmpty 没有获取到重复的记录");
                return AjaxResult.success("没有获取到重复的记录");
            }

            // 数据分组 按照uniqueId
            Map<String, List<CrmOutCallCdr>> collectMap = crmOutCallCdrList.stream().filter(item -> StringUtils.isNotEmpty(item.getCdrMainUniqueId())).collect(Collectors.groupingBy(CrmOutCallCdr::getCdrMainUniqueId));
            log.info("cdrBatchUpdateRecord.collectMap.size ={}", collectMap.size());
            // 保留数据/ 推送的数据
            List<CrmOutCallCdr> retainData = new ArrayList<>();
            // 导入的数据
            Map<String, CrmOutCallCdr> importData = new HashMap<>();
            // 记录待更新的数据
            Set<Long> ids = new HashSet<>();

            Iterator<Map.Entry<String, List<CrmOutCallCdr>>> entries = collectMap.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry<String, List<CrmOutCallCdr>> entry = entries.next();
                List<CrmOutCallCdr> crmOutCallCdrs = entry.getValue();
                if (!crmOutCallCdrs.isEmpty() && crmOutCallCdrs.size() == 2) {
                    for (CrmOutCallCdr outCallCdr : crmOutCallCdrs) {
                        ids.add(outCallCdr.getId());
                        if (StringUtils.isNotEmpty(outCallCdr.getCdrCustomerAreaCode()) && (outCallCdr.getUserId() == null || outCallCdr.getDeptId() == null)) {
                            retainData.add(outCallCdr);
                        } else {
                            importData.put(outCallCdr.getCdrMainUniqueId(), outCallCdr);
                        }
                    }
                }

            }
            log.info("cdrBatchUpdateRecord.ids.data ={}", ids);
            log.info("cdrBatchUpdateRecord.retainData.size ={}", retainData.size());
            log.info("cdrBatchUpdateRecord.importData.size ={}", importData.size());

            List<CrmOutCallCdr> temp = new ArrayList<>();
            if (retainData.isEmpty()) {
                return AjaxResult.success("没有获取到需要更新的数据");
            }
            // 重新赋值
            for (int i = 0; i < retainData.size(); i++) {

                CrmOutCallCdr item = retainData.get(i);

                CrmOutCallCdr callCdr = importData.get(item.getCdrMainUniqueId());
                if (callCdr == null) {
                    log.info("cdrBatchUpdateRecord.foreach.cdrMainUniqueId ={}", item.getCdrMainUniqueId());
                    continue;
                }
                // 批量更新
                CrmOutCallCdr updateCdr = new CrmOutCallCdr();
                updateCdr.setMchId(callCdr.getMchId());
                updateCdr.setDeptId(callCdr.getDeptId());
                updateCdr.setUserId(callCdr.getUserId());
                updateCdr.setId(item.getId());
                temp.add(updateCdr);

                if (i == retainData.size() - 1 || ((i != 0) && (i % 100 == 0))) {//NOSONAR
                    if (retainData.size() > 0) {//NOSONAR
                        crmOutCallCdrMapper.batchUpdateOutCallRecord(temp);
                        temp = new ArrayList<>();
                    }
                }

            }
        } catch (Exception e) {
            log.error("cdrBatchUpdateRecord.Exception={}", e);
        }
        return AjaxResult.success();
    }


    @DataScope(deptAlias = "c")
    @Override
    public List<CrmOutCallCdr> selectCrmOutCallCdrListExport(CrmOutCallCdr crmOutCallCdr) {
        LoginUser user = SecurityUtils.getLoginUser();

        // 如果是管理岗位，取所管理的部门，如果不是管理岗位，取个人外呼记录
        boolean result = adminManager.loginUserIsLeader(user);
        if (!result) {
            crmOutCallCdr.setUserId(user.getUserId());
        }

        // 如果是分公司管理员或者质检，取分公司所有数据
        if (adminManager.loginUserIsBranchBoss(user) && !"super".equals(user.getUsername())) {
            crmOutCallCdr.setMchId(user.getUser().getMchId());
            if(crmOutCallCdr.getUserId() != null){
                crmOutCallCdr.setUserId(crmOutCallCdr.getUserId());
            }
        }

        //客服质检 电销 看全公司
        if (adminManager.loginUserIsServiceDept(user) || user.getUser().getMchId().intValue() == 1063) {
            crmOutCallCdr.setUserId(null);
            crmOutCallCdr.setMchId(user.getUser().getMchId());
            if (crmOutCallCdr.getParams() !=null && crmOutCallCdr.getParams().get("dataScope")!=null) {
                crmOutCallCdr.getParams().put("dataScope", "");
            }
        }

        // 质检  开全集团的
        if ( user.getUser().getMchId().intValue() == 1090) {
            crmOutCallCdr.setUserId(null);
            crmOutCallCdr.setMchId(null);
            if (crmOutCallCdr.getParams() !=null && crmOutCallCdr.getParams().get("dataScope")!=null) {
                crmOutCallCdr.getParams().put("dataScope", "");
            }
        }
        return crmOutCallCdrMapper.selectCrmOutCallCdrList(crmOutCallCdr);

    }

    /**
     * Description: 分割list集合
     *
     * @param list      集合数据
     * @param splitSize 几个分割一组
     * @return 集合分割后的集合
     */
    public static <T> List<List<T>> splitList(List<T> list, int splitSize) {
        //判断集合是否为空
        if (CollectionUtils.isEmpty(list))
            return Collections.emptyList();
        //计算分割后的大小
        int maxSize = (list.size() + splitSize - 1) / splitSize;
        //开始分割
        return Stream.iterate(0, n -> n + 1)
                .limit(maxSize)
                .parallel()
                .map(a -> list.parallelStream().skip(a * splitSize).limit(splitSize).collect(Collectors.toList()))
                .filter(b -> !b.isEmpty())
                .collect(Collectors.toList());
    }

    /**
     * 调用天润返回录音地址
     *
     * @param paramMap
     */
    private String getCommonRecordUrl(JSONObject paramMap) {
        SysDeptToken sysDeptToken = sysDeptTokenService.getSysDeptTokenByEnterpriseId(paramMap.getString("cdrEnterpriseId"));
        log.info("录音信息,根据企业id 获取token值 sysDeptToken ={}", JSONObject.toJSONString(sysDeptToken));
        // 拼接请求
        long timestamp = new Date().getTime() / 1000;
        String recordUrl = String.format(CrmConstants.TR_CALL_RECORD_URL, region, version);
        // 如果是rtc 外呼获取录音默认走生产 5平台 否则测试环境获取录音有问题 为了兼容测试环境
        if(CrmConstants.TR_CDR_CALL_TYPE_6.equals(paramMap.get("cdrCallType")) || "7501350".equals(paramMap.getString("cdrEnterpriseId"))){
            recordUrl = String.format(CrmConstants.TR_CALL_RECORD_URL, 5, "v10");
        }
        String recordType = paramMap.getString("recordType");
        StringBuilder sb = new StringBuilder();
        sb.append("?");
        sb.append("validateType=2");
        sb.append("&enterpriseId=" + paramMap.getString("cdrEnterpriseId"));
        sb.append("&timestamp=" + timestamp);
        //record:录音 voicemail:留言
        sb.append("&recordType=" + recordType);
        sb.append("&recordFile=" + paramMap.getString("recordFile"));
        sb.append("&sign=" + Md5Utils.hash(paramMap.getString("cdrEnterpriseId") + timestamp + sysDeptToken.getTrLoginToken()));
        String url = recordUrl.concat(sb.toString());
        log.info("recordUrl = {}", url);
        String recordsResult = HttpUtil.get(url);
        log.info("Execute result[{}]", recordsResult);
        return recordsResult;
    }

    /**
     * 根据企业id 和 uniqueId 获取【外呼通话记录】
     *
     * @param crmCallRecordCallBackVo 【外呼录音回调实体】
     * @return 结果
     */
    @Override
    public AjaxResult getRecordCallBack(CrmCallRecordCallBackVo crmCallRecordCallBackVo) {
        try {
            String recordURL = null;
            JSONObject paramObj = new JSONObject();
            // 处理外呼录音
            if (CrmConstants.TR_CDR_CALL_TYPE_4.equals(crmCallRecordCallBackVo.getCdrCallType())) {
                // 1、 根据用户id 和 uniqueId 获取话单记录
                CrmOutCallCdr crmOutCallCdr = crmOutCallCdrMapper.selectCrmOutCallRecordByUniqueId(crmCallRecordCallBackVo);
                log.info("根据企业id 和 uniqueId 获取【外呼通话记录】 crmOutCallCdr = {}", JSONObject.toJSONString(crmOutCallCdr));
                if (Objects.nonNull(crmOutCallCdr) && StringUtils.isNotEmpty(crmOutCallCdr.getCdrRecordFile1()) && StringUtils.isNotEmpty(crmOutCallCdr.getCdrEnterpriseId())) {

                    paramObj.put("cdrEnterpriseId", crmOutCallCdr.getCdrEnterpriseId());
                    paramObj.put("recordFile", crmOutCallCdr.getCdrRecordFile1());
                    paramObj.put("cdrCallType", CrmConstants.TR_CDR_CALL_TYPE_4);
                    paramObj.put("id", crmOutCallCdr.getId());
                    paramObj.put("recordType", crmCallRecordCallBackVo.getRecordType());
                    // 2、 获得到文件名称后 调天润接口 获取录音文件信息 上传到cos
                    String recordsResult = this.getCommonRecordUrl(paramObj);
                    if (StringUtils.isNotEmpty(recordsResult)) {
                        JSONObject resObject = JSONObject.parseObject(recordsResult);
                        // result    Int	0 成功, -1 失败
                        if (Objects.nonNull(resObject) && resObject.getIntValue("result") == 0) {
                            // 试听或下载地址
                            recordURL = resObject.getString("data");
                        }
                    }
                }
            }
            // 处理RTC外呼
            if (CrmConstants.TR_CDR_CALL_TYPE_6.equals(crmCallRecordCallBackVo.getCdrCallType())) {
                // 1、 根据用户id 和 uniqueId 获取话单记录
                CrmOutCallCdr crmOutCallCdr = crmOutCallCdrMapper.selectCrmOutCallRecordByUniqueId(crmCallRecordCallBackVo);
                log.info("根据企业id 和 uniqueId 获取【外呼通话记录】 crmOutCallCdr = {}", JSONObject.toJSONString(crmOutCallCdr));
                if (Objects.nonNull(crmOutCallCdr) && StringUtils.isNotEmpty(crmOutCallCdr.getCdrRecordFile1()) && StringUtils.isNotEmpty(crmOutCallCdr.getCdrEnterpriseId())) {

                    paramObj.put("cdrEnterpriseId", crmOutCallCdr.getCdrEnterpriseId());
                    paramObj.put("recordFile", crmOutCallCdr.getCdrRecordFile1());
                    paramObj.put("cdrCallType", CrmConstants.TR_CDR_CALL_TYPE_6);
                    paramObj.put("id", crmOutCallCdr.getId());
                    paramObj.put("recordType", crmCallRecordCallBackVo.getRecordType());
                    // 2、 获得到文件名称后 调天润接口 获取录音文件信息 上传到cos
                    String recordsResult = this.getCommonRecordUrl(paramObj);
                    if (StringUtils.isNotEmpty(recordsResult)) {
                        JSONObject resObject = JSONObject.parseObject(recordsResult);
                        // result    Int	0 成功, -1 失败
                        if (Objects.nonNull(resObject) && resObject.getIntValue("result") == 0) {
                            // 试听或下载地址
                            recordURL = resObject.getString("data");
                        }
                    }
                }
            }
            // 处理回呼录音
            if (CrmConstants.TR_CDR_CALL_TYPE_1.equals(crmCallRecordCallBackVo.getCdrCallType())) {
                CrmCallBackCdr crmCallBackCdrVo = new CrmCallBackCdr();
                crmCallBackCdrVo.setCdrEnterpriseId(crmCallRecordCallBackVo.getCdrEnterpriseId());
                crmCallBackCdrVo.setCdrMainUniqueId(crmCallRecordCallBackVo.getCdrUniqueId());
                List<CrmCallBackCdr> crmCallBackCdrs = crmCallBackCdrService.selectCrmOutCallRecordByUniqueId(crmCallBackCdrVo);
                if (Objects.nonNull(crmCallBackCdrs) && crmCallBackCdrs.size() > 0) {
                    CrmCallBackCdr crmCallBackCdr = crmCallBackCdrs.get(0);
                    paramObj.put("cdrEnterpriseId", crmCallBackCdr.getCdrEnterpriseId());
                    paramObj.put("recordFile", crmCallRecordCallBackVo.getRecordFile());
                    paramObj.put("cdrCallType", CrmConstants.TR_CDR_CALL_TYPE_1);
                    paramObj.put("id", crmCallBackCdr.getId());
                    paramObj.put("recordType", crmCallRecordCallBackVo.getRecordType());

                    String recordsResult = this.getCommonRecordUrl(paramObj);
                    if (StringUtils.isNotEmpty(recordsResult)) {
                        JSONObject resObject = JSONObject.parseObject(recordsResult);
                        // result    Int	0 成功, -1 失败
                        if (Objects.nonNull(resObject) && resObject.getIntValue("result") == 0) {
                            // 试听或下载地址
                            recordURL = resObject.getString("data");
                        }
                    }
                }
            }
            // 如果从天润获取到了录音地址 上传录音到 cos
            if (StringUtils.isNotEmpty(recordURL)) {
                JSONObject jsonObjectResult = this.uploadFileToCos(recordURL, paramObj.get("recordFile") + CrmConstants.TR_RECORD_FILE_SUFFIX, recordURL.length() + "");
                log.info("cos 录音文件地址 jsonObjectResult = {}", jsonObjectResult.toJSONString());
                // 3、更新外呼通话记录cos_record_rul 字段
                Map<String, Object> map = new HashMap<>();

                map.put("id", paramObj.getLong("id"));
                map.put("recordUrl", jsonObjectResult.getString("cosUrl"));
                map.put("fileSize", jsonObjectResult.getIntValue("fileSize"));

                if (CrmConstants.TR_CDR_CALL_TYPE_4.equals(paramObj.getString("cdrCallType"))) {
                    // 更新外呼录音文件
                    int count = crmOutCallCdrMapper.updateCrmOutCallCdrById(map);
                    if (count > 0) {
                        return AjaxResult.success("外呼录音文件获取成功");
                    }
                }

                if (CrmConstants.TR_CDR_CALL_TYPE_6.equals(paramObj.getString("cdrCallType"))) {
                    // 更新外呼录音文件
                    int count = crmOutCallCdrMapper.updateCrmOutCallCdrById(map);
                    if (count > 0) {
                        return AjaxResult.success("RTC外呼录音文件获取成功");
                    }
                }

                if (CrmConstants.TR_CDR_CALL_TYPE_1.equals(paramObj.getString("cdrCallType"))) {
                    map.put("recordType", crmCallRecordCallBackVo.getRecordType());
                    int count = crmCallBackCdrService.updateCrmCallBackCdrById(map);
                    if (count > 0) {
                        return AjaxResult.success("回呼录音文件获取成功");
                    }

                }

            } else {
                log.info("获取录音，从天润获取录音地址失败 cdrUniqueId={}",crmCallRecordCallBackVo.getCdrUniqueId());
            }

        } catch (Exception e) {
            log.error("通话录音获取失败 e = {}", e);
        }
        return AjaxResult.error("通话录音获取失败");
    }


    /**
     * 上传文件到COS
     *
     * @param url
     * @param length
     */
    private JSONObject uploadFileToCos(String url, String fileName, String length) {
        JSONObject jsonObject = new JSONObject();
        //下载文件
        log.info("Execute Url[{}]", url);
        byte[] fileBytes = FileUtils.getFileBytesByUrl(url, Long.parseLong(length));
        log.info("Execute result[{}]", fileBytes == null ? 0 : fileBytes.length);
        jsonObject.put("fileSize", fileBytes == null ? 0 : fileBytes.length);
        //上传
        String res = UploadImgUtils.uploadFile(uploadUrl, fileName);
        log.debug("上传文件到COS,源文件地址[{}] 文件名称[{}] COS地址[{}]", new Object[]{url, fileName, res});
        jsonObject.put("cosUrl", res);
        return jsonObject;
    }

}
